#ifndef RPC_HELPER_H_
#define RPC_HELPER_H_

#include "base/base.h"
#include "include/rpc_def.h"

#define URPC_MAGIC 0x012343210
#define INVALIED_FUNC_ID (-1)

namespace urpc {
struct RpcHeader {
    uint32 magic_num;
    uint32 id;
    uint32 length;
    uint32 func_id;
//    uint32 hash;
};

#define HEADER_LENGTH (sizeof(RpcHeader))

struct Handler {
    Service* service;
    const MethodDescriptor* method_descriptor;
    const Message* request;
    const Message* response;
};

class HandlerMap {
  public:
    explicit HandlerMap(Service* service) {
      AddService(service);
    }
    ~HandlerMap();

    void AddService(Service* service);

    Handler* FindHandlerById(uint32 id) {
      std::map<uint32, Handler*>::iterator it = handler_map_.find(id);
      if (it == handler_map_.end()) {
        LOG(WARNING) << "can't find handler, id: " << id;
        return NULL;
      }

      return it->second;
    }

    static uint32 GetFunidByMethodDescriptor(const MethodDescriptor* mdesc);

  private:
    std::map<uint32, Handler*> handler_map_;

    DISALLOW_COPY_AND_ASSIGN(HandlerMap);
};

inline uint32 GetNextRpcId() {
  static uint32 id = 0;
  return ++id;
}
}

#endif
